﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Collections;

using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Web;

using System.Xml.Xsl;
using System.Xml.Schema;

using System.Diagnostics;
using System.Reflection;
using System.Threading; 

namespace SchematronTestFramework
{

    class ThreadFunction
    {
        public AssertMeta meta;

        public string xml;
        public string rep;
        public string sch;

        public ThreadFunction(AssertMeta am, string x, string r, string s)
        {
            meta = am;
            xml = x;
            rep = r;
            sch = s;
        }

        public void ThreadMain()
        {
            meta.run(xml, rep, sch);
        }

    }
    class RunTest
    {
        //static List<AssertMeta> metas = new List<AssertMeta>();

        static void threadFunc()
        {
            
            //metas.Add(meta);
        }

        public static List<string> files = new List<string>();

        public static void analyzeFiles(string path)
        {

            FileInfo fileInfo = new FileInfo(path);

            if ((fileInfo.Attributes & FileAttributes.Directory) != 0)
            {
                DirectoryInfo dir = new DirectoryInfo(path);


                FileSystemInfo[] f = dir.GetFileSystemInfos();
                for (int i = 0; i < f.Length; i++)
                {
                    FileInfo file = new FileInfo(f[i].FullName);


                    if ((file.Attributes & FileAttributes.Directory) != 0)
                    {
                        analyzeFiles(f[i].FullName);
                    }
                    else
                    {
                        //Console.WriteLine(file.FullName + "\t " + file.Length);
                        if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".sch")
                        {
                            files.Add(file.FullName);
                            //MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
                        }

                    }


                }
            }
            else
            {
                if (!(fileInfo.Exists))
                {
                    return;
                }
                files.Add(path);
            }




        }
        static void Main(string[] args)
        {

            string schematron="";
            string xmlfile="";
            string report="";

            if (args.Length <= 0)
            {
                Console.WriteLine("Error in command line.");
                return;
            }
            else if (args.Length % 2 == 1)
            {
                Console.WriteLine("Error in command line.");
                return;
            }

            int i = 0;
            while (i < args.Length)
            {
                if (args[i].CompareTo("-s") == 0)
                {
                    break;
                }
                i++;
            }
            if (i == args.Length)
            {
                Console.WriteLine("Error in command line, -s required.");
                return;
            }
            else if (i == (args.Length-1))
            {
                Console.WriteLine("Error in command line, -s value required.");
                return;
            }
            else
            {
                schematron = args[i + 1];
            }


            i = 0;

            while (i < args.Length)
            {
                if (args[i].CompareTo("-x") == 0)
                {
                    break;
                }
                i++;
            }
            if (i == args.Length)
            {
                Console.WriteLine("Error in command line, -x required.");
                return;
            }
            else if (i == (args.Length - 1))
            {
                Console.WriteLine("Error in command line, -x value required.");
                return;
            }
            else
            {
                xmlfile = args[i + 1];
            }

            i = 0;

            while (i < args.Length)
            {
                if (args[i].CompareTo("-o") == 0)
                {
                    break;
                }
                i++;
            }
            if (i == args.Length)
            {
                Console.WriteLine("Error in command line, -o required.");
                return;
            }
            else if (i == (args.Length - 1))
            {
                Console.WriteLine("Error in command line, -o value required.");
                return;
            }
            else
            {
                report = args[i + 1];
            }
            Reporter reporter = new Reporter(report);
            reporter.GenerateXmlReport();

            analyzeFiles(schematron);
            foreach (string schfile in files)
            {
                GetAssertMetas test = new GetAssertMetas(schfile);

                List<AssertMeta> lists = new List<AssertMeta>();

                lists = test.split();



                List<Thread> threads = new List<Thread>();



                foreach (AssertMeta m in lists)
                {


                    ThreadFunction tf = new ThreadFunction(m, xmlfile, report, schfile);

                    Thread t = new Thread(tf.ThreadMain);
                    threads.Add(t);
                    t.Start();

                }

                foreach (Thread th in threads)
                {
                    th.Join();
                }


                lists.Clear();

            }
            Console.WriteLine("********************************  Summary  ********************************");
            Console.WriteLine("Passed: " + TestCommand.passed);
            Console.WriteLine("False Negative: " + TestCommand.fn);
            Console.WriteLine("False Positive: " + TestCommand.fp);
            Console.WriteLine("Error: " + TestCommand.error);
            Console.WriteLine("Not Run: " + TestCommand.notrun);

            TestCommand.passed = 0;
            TestCommand.fp = 0;
            TestCommand.error = 0;
            TestCommand.notrun = 0;


            reporter.EndReport();
            Console.ReadKey();

        }
    }
 
}
